﻿@model dynamic
@{
    Layout = null;
}

<link rel="stylesheet" href="/resources/skin/zTree/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="/resources/script/jquery.ztree/jquery.ztree.core.js"></script>
<script type="text/javascript" src="/resources/script/jquery.ztree/jquery.ztree.excheck.js"></script>

<div class="home-desktop">
    <div class="content-pages-title">
        <h1>角色权限</h1>
    </div>
    <div class="content-notice-body">
        <div class="modular-info" style="display: block;">
            <table style="width: 100%;  border: none;">
                <tr>
                    <td valign="top">
                        <div class="notice-box-left">
                            <div class="modular-info" style="display: block;">
                                <ul class="form">
                                    <li>
                                        <div class="zTreeDemoBackground left">
                                            <ul id="treeDemo" class="ztree"></ul>
                                        </div>
                                    </li>
                                    <li class="row-last">
                                        <em>&nbsp;</em>&nbsp;&nbsp;
                                        <button type="button" class="cotrlBtn btnBlue" id="btn_save">保存</button>
                                        <input type="hidden" value="@ViewBag.roleID" id="hdRoleID" />
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </div>
    </div>

</div>
<SCRIPT type="text/javascript">
    var setting = {
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };

    //var zNodes = [
    //    { id: '1a', pId: '0', name: "随意勾选 1", open: true },
    //    { id: '11', pId: '1a', name: "随意勾选 1-1", open: true },
    //    { id: '111', pId: '11', name: "随意勾选 1-1-1" },
    //    { id: '112', pId: '11', name: "随意勾选 1-1-2" },
    //    { id: '12', pId: '1', name: "随意勾选 1-2", open: true },
    //    { id: '121', pId: '12', name: "随意勾选 1-2-1" },
    //    { id: '122', pId: '12', name: "随意勾选 1-2-2" },
    //    { id: '2', pId: '0', name: "随意勾选 2", checked: true, open: true },
    //    { id: '21', pId: '2', name: "随意勾选 2-1" },
    //    { id: '22', pId: '2', name: "随意勾选 2-2", open: true },
    //    { id: '221', pId: '22', name: "随意勾选 2-2-1", checked: true },
    //    { id: '222', pId: '22', name: "随意勾选 2-2-2" },
    //    { id: '23', pId: '2', name: "随意勾选 2-3" }
    //];

    var code;

    function setCheck() {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
            py ="p",
            sy = "s",
            pn = "p" ,
            sn = "s",
            type = { "Y": py + sy, "N": pn + sn };
        zTree.setting.check.chkboxType = type;
        showCode('setting.check.chkboxType = { "Y" : "' + type.Y + '", "N" : "' + type.N + '" };');
    }
    function showCode(str) {
        if (!code) code = $("#code");
        code.empty();
        code.append("<li>" + str + "</li>");
    }

    $("#btn_save").click(function () {
        var zTreeOjb = $.fn.zTree.getZTreeObj("treeDemo");
        var checkedNodes = zTreeOjb.getCheckedNodes();

        var functionIds = new Array();
        if (checkedNodes != null && checkedNodes.length > 0) {
            for (var i = 0; i < checkedNodes.length; i++) {
                functionIds.push(checkedNodes[i].id);
            }
        }

        $.post("@Url.Action("SaveRoleFunction")", { roleId: $("#hdRoleID").val(), functionIds: functionIds }, function (result) {
                if (result.code == 0) {
                    dcAlert(result.message, 0, 1, function () {
                        $(".layer-dialog-close-btn").click();
                    });
                }
                else {
                    dcAlert(result.message, 0, 2);
                }
            });
    });

    $(document).ready(function () {
        //获取角色对应的权限
        $.post("@Url.Action("GetFunctionByRoleID")", { roleId: $("#hdRoleID").val() }, function (result) {
            if (result.code == 0) {
                $.fn.zTree.init($("#treeDemo"), setting, result.data);
                //$.fn.zTree.init($("#treeDemo"), setting, zNodes);
                setCheck();
                $("#py").bind("change", setCheck);
                $("#sy").bind("change", setCheck);
                $("#pn").bind("change", setCheck);
                $("#sn").bind("change", setCheck);
            }
            else {
                dcAlert(result.message, 0, 2);
            }
        });
        //var zTreeOjb = $.fn.zTree.getZTreeObj("treeDemo");
        //var checkedNodes = zTreeOjb.getCheckedNodes();
        //console.log(checkedNodes);
    });
</SCRIPT>
